package nl.windgazer.arachne.components.socialmedia.facebook;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import nl.windgazer.arachne.services.facebook.FacebookShareLinkType;

import org.apache.tapestry5.annotations.Environmental;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.services.javascript.JavaScriptSupport;

/**
 * This component attempts to implement most of the functionality available for the
 * http://developers.facebook.com/docs/reference/plugins/like, when not supporting
 * the JavaScript SDK on the page.<br />
 * The only thing currently not supported is alternate font-faces.<br />
 * @author mreuring
 *
 */
public class FacebookLikeBox {

	//Injects and Persists

	@Environmental
	private JavaScriptSupport _js;

	//Properties and Parameters

	@Parameter(required=true)
	private Long pageId;

	/**
	 * The width of the iframe
	 */
	@Parameter
	@Property
	private int width = 450;

	@Parameter
	private int connections = 10;

	@Parameter
	private boolean stream = true;

	@Parameter
	private boolean header = true;

	/**
	 * This value is pre-calculated based on the other options for this box.
	 * 255 is the height in pixels with all options disabled, Stream adds 300 pixels,
	 * header adds (another) 32 pixels.
	 */
	@Property(write=false)
	private int height = 255;

	@SuppressWarnings("unused")
	@Parameter
	@Property
	private FacebookShareLinkType type = FacebookShareLinkType.button_count;

	private StringBuilder query;

	//Components

	//Events

	@SetupRender
	void setupMetaTags() throws UnsupportedEncodingException {
	
		//Create new StringBuilder (for thread-safety, do it here)
		query = new StringBuilder();

		// Add link to FB-share script @ http://static.ak.fbcdn.net/connect.php/js/FB.Share
		_js.importJavaScriptLibrary("http://static.ak.fbcdn.net/connect.php/js/FB.Share");
		setHeight();
		buildQuery();

	}

	//Other (check for business-logic, it should not remain here)

	public void setHeight() {
		if (stream) {
			height += 300;
		}
		if (header) {
			height += 32;
		}
	}
	
	/**
	 *              http://www.facebook.com/plugins/likebox.php?id=115526181791627&amp;width=450&amp;connections=10&amp;stream=true&amp;header=true&amp;height=587
	 * <iframe src="http://www.facebook.com/plugins/likebox.php?id=115526181791627&amp;width=300&amp;connections=10&amp;stream=true&amp;header=true&amp;height=587" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:587px;" allowTransparency="true"></iframe>
	 * <iframe src="http://www.facebook.com/plugins/likebox.php?id=185550966885&amp;width=300&amp;connections=10&amp;stream=true&amp;header=true&amp;height=587" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:587px;" allowTransparency="true"></iframe>
	 */
	private void buildQuery() throws UnsupportedEncodingException {
		addQuery("id",String.valueOf(pageId));
		addQuery("width", String.valueOf(width));
		addQuery("connections", String.valueOf(connections));
		addQuery("stream", String.valueOf(stream));
		addQuery("header", String.valueOf(header));
		addQuery("height", String.valueOf(height));
	}

	private void addQuery(String key, String value) throws UnsupportedEncodingException {
		if (query.length() == 0) {
			query.append('?');
		} else {
			query.append('&');
		}
		query.append(URLEncoder.encode(key, "UTF-8"));
		query.append('=');
		query.append(URLEncoder.encode(value, "UTF-8"));
	}

	public String getQuery() {
		final String q = query.toString();
		query.setLength(0);
		return q;
	}
}
